# -*- coding: utf-8 -*-
# @Author	: starview.guojian.ma
# @Date		: 2022/5/23 15:24
# @Last Modified by:   starview.guojian.ma
# @Last Modified time: 2022/5/23 15:24
# Thanks for your comments!

from typing import Dict, Type, Optional, List, Any

from pydantic import validator

from src.basedata.domain.value_object import DepartmentCategory, PersonType, PersonGender, MedicineCNWestFlag, \
    MedicineAntibacterialType
from src.seedwork.application.commands import Command
from src.seedwork.domain.entities import PyObjectId


class HealthCheck(Command):
    pass


class SyncDepartments(Command):
    # 具体含义参考处方对接文档
    No: Optional[int]  # 标识第几条数据
    Code: str  # 科室代码
    Name: str  # 科室名称
    Category: Optional[DepartmentCategory]  # 科室类型
    Address: str = ''  # 地址
    ParentCode: Optional[str]  # 上级科室代码
    NameAcronym: Optional[str]
    NameSpell: Optional[str]


class SyncPersons(Command):
    No: Optional[int]  # 序号
    Code: str  # 员工编号
    Name: str  # 员工名称
    Type: PersonType  # 员工类别
    Gender: PersonGender = PersonGender.unknown  # 性别
    DeptCode: str  # 科室
    Acronymy: str = ''  # 拼音代码
    Birthdate: str = ''  # 生日
    Telephone: str = ''  # 联系方式
    HISCode: str = ''  # HIS员工代码
    Title: str = ''  # 职称
    Country: str = ''  # 国家
    IDNumber: str = ''  # 身份证号

    # Pydantic v1 中使用 @validator（而非 field_validator）
    @validator('Gender', pre=True)  # pre=True 表示在默认验证前执行
    def handle_empty_gender(cls, v):
        # 处理空字符串，转为默认值"0"
        if not v:
            return PersonGender.unknown.value
        return v


class SyncMedicines(Command):
    No: Optional[int]  # 序号
    Code: str  # 药品编码
    BarCode: str = ''  # 药品条形码
    MedNationInsCode: str = ''  # 药品国家医保统一编码
    Name: str  # 药品通用名
    EnglishName: str = ''  # 英文名
    TradeName: str = ''  # 商品名
    ChemistryName: str = ''  # 化学名
    Acronymy: str  # 拼音代码
    ApprovalNo: str  # 批准文号
    Spec: str  # 药品规格
    Uom: str  # 药品剂量单位
    DosageForm: Optional[str]  # 医院药品剂型
    Manufacturer: str  # 生产厂家
    PackageSpec: str = ''  # 包装规格
    PackageUom: str = ''  # 包装单位
    PackageMinUom: str = ''  # 最小包装单位
    CNWestFlag: MedicineCNWestFlag  # 中西药标识
    BasicDrug: str = "0"  # 是否为基药 0-非基药、1-基药
    Antibacterial: str = "0"  # 是否是抗菌药物 0-非抗菌、1-抗菌
    AntibacType: MedicineAntibacterialType = MedicineAntibacterialType.NON_ANTIBACTERIAL  # 抗菌药物分级
    Injection: str = '0'  # 0-非针剂、1-针剂      是否针剂
    Antitumor: str = "0"  # 抗癌药物 0-非抗癌药物、1-抗癌药物
    Toxic: str = '0'  # 毒性药物 0-非毒性药物、1-毒性药物
    DDD: str = ''  # DDD值
    TotalQTY: str = ''
    PackgeMinQTY: str = ''


class SyncRouteOfAdministration(Command):
    No: Optional[int]
    Code: str  # 用法编码
    Name: str  # 用法名称
    Acronymy: str = ''  # 拼音代码


class SyncAdministrationFreq(Command):
    No: Optional[int]  # 序号
    Code: str  # 频次编码
    Name: str  # 频次名称
    Acronymy: str = ''  # 拼音代码
    DailyCount: Optional[int]  # 每日次数
    Interval: Optional[str]  # 最小周期
    TakeTime: Optional[str]  # 执行时间


class SyncDiseaseCode(Command):
    No: Optional[int]  # 序号
    Code: str  # 诊断编码
    Name: str  # 诊断名称
    Fjm: str = ''  # 附加码
    Acronymy: str = ''


class SyncBasedata(Command):
    app_id: Optional[str]
    method_name: Optional[str]


class UpdatePersonCache(Command):
    data: Dict[str, Any]


class QueryAppDepartments(Command):
    app_id: str


class QueryAppPersons(Command):
    app_id: str


class QueryAppMedicines(Command):
    app_id: str


class QueryAppAdministrationFreq(Command):
    app_id: str


class QueryAppRouteOfAdministration(Command):
    app_id: str


class QueryAppDiseaseCode(Command):
    app_id: str


class SyncAppDepartments(Command):
    app_id: str


class SyncAppPersons(Command):
    app_id: str


class SyncAppMedicines(Command):
    app_id: str


class SyncAppAdministrationFreq(Command):
    app_id: str


class SyncAppRouteOfAdministration(Command):
    app_id: str


class SyncAppDiseaseCode(Command):
    app_id: str


COMMAND_METHODS_MAP: Dict[str, Type[Command]] = {
    "health_check": HealthCheck,
    "sync_app_department_basedata": SyncAppDepartments,
    "sync_app_person_basedata": SyncAppPersons,
    "sync_app_medicine_basedata": SyncAppMedicines,
    "sync_app_route_of_administration_basedata": SyncAppRouteOfAdministration,
    "sync_app_administration_freq_basedata": SyncAppAdministrationFreq,
    "sync_app_disease_code_basedata": SyncAppDiseaseCode,
    'sync_person': UpdatePersonCache,

    "query_app_department_basedata": QueryAppDepartments,
    "query_app_person_basedata": QueryAppPersons,
    "query_app_medicine_basedata": QueryAppMedicines,
    "query_app_route_of_administration_basedata": QueryAppRouteOfAdministration,
    "query_app_administration_freq_basedata": QueryAppAdministrationFreq,
    "query_app_disease_code_basedata": QueryAppDiseaseCode,

}
